/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.gui;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.plaf.basic.BasicArrowButton;

/**
 *
 * @author eisental
 */
public class GUIArrowButton extends BasicArrowButton {
    Color color, darker;

    public GUIArrowButton(int orientation, Color color) {
        super(orientation);
        this.color = color;
        this.darker = color.darker();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        int w = getSize().width;
        int h = getSize().height;
	    Color origColor = g.getColor();
	    boolean isPressed = getModel().isPressed();
	    boolean isEnabled = isEnabled();

        g.setColor(Color.BLACK);
        g.fillRect(0, 0, w, h);

        // If there's no room to draw arrow, bail
        if(h < 3 || w < 3)      {
            g.setColor(origColor);
            return;
        }

        // Draw the arrow
        if (isPressed) g.setColor(color);
        else g.setColor(darker);
        int size = Math.min(h / 2, w / 2);
        size = Math.max(size, 2);
	    paintTriangle(g, (w - size) / 2, (h - size) / 2,
                size, direction, isEnabled);
        switch (direction) {
            case NORTH:
                g.drawLine(0, h-1, w, h-1);
                break;
            case SOUTH:
                g.drawLine(0, 0, w, 0);
                break;
            case WEST:
                g.drawLine(w-1, 0, w-1, h);
                break;
            case EAST:
                g.drawLine(0, 0, 0, h);
                break;
        }
	    g.setColor(origColor);
    }

    @Override
	public void paintTriangle(Graphics g, int x, int y, int size,
					int direction, boolean isEnabled) {
	    int mid, i, j;

	    j = 0;
        size = Math.max(size, 2);
	    mid = (size / 2) - 1;

	    g.translate(x, y);

        switch(direction)       {
            case NORTH:
                for(i = 0; i < size; i++)      {
                    g.drawLine(mid-i, i, mid+i, i);
                }
                g.drawLine(mid-i+2, i, mid+i, i);
                break;
            case SOUTH:
                j = 0;
                for(i = size-1; i >= 0; i--)   {
                    g.drawLine(mid-i, j, mid+i, j);
                    j++;
                }
                break;
            case WEST:
                for(i = 0; i < size; i++)      {
                    g.drawLine(i, mid-i, i, mid+i);
                }
                break;
            case EAST:
                j = 0;
                for(i = size-1; i >= 0; i--)   {
                    g.drawLine(j, mid-i, j, mid+i);
                    j++;
                }
                break;
        }
	    g.translate(-x, -y);	
	}

}
